import pandas as pd

from utils.common import *
from datetime import datetime, timedelta


class LoanUnOnloanV2():

    def __init__(self, loan_un_base_v2):
        self.acq_channel = loan_un_base_v2.acq_channel
        self.app_order_id = loan_un_base_v2.app_order_id
        self.product_code = loan_un_base_v2.product_code
        self.current_amount = loan_un_base_v2.current_amount
        self.current_days_per_term = loan_un_base_v2.current_days_per_term
        self.current_term = loan_un_base_v2.current_term
        self.order_apply_time = loan_un_base_v2.order_apply_time
        self.order_apply_time_datetime = loan_un_base_v2.order_apply_time_datetime
        self.order_apply_date = loan_un_base_v2.order_apply_date
        self.order_data_df = loan_un_base_v2.order_data_df
        self.installments_data_df = loan_un_base_v2.installments_data_df
        self.contract_data_df = loan_un_base_v2.contract_data_df

    def gen_features(self):
        feature_dict = {}
        day_list = [1, 3, 7, 15, 30, 60, 90, 180, 360]
        cnt_list = [1, 3, 7, 15, 30]
        order_type_list = [('acq_channel', self.acq_channel, True), ('acq_channel', self.acq_channel, False), ('product_code', self.product_code, True), ('product_code', self.product_code, False), 'all']
        time_type_list = [(0, 5), (6, 12), (13, 18), (19, 23)]
        day_type_list = [('is_workday', 1), ('is_workday', 0), ('is_holiday', 1), ('is_holiday', 0)]

        for day in day_list:
            tmp1_installments_data_df = self.installments_data_df[self.installments_data_df['day_interval'] <= day]
            for order_type in order_type_list:
                if order_type == 'all':
                    tmp2_installments_data_df = tmp1_installments_data_df
                    suffix = f'd{day}_all'
                    self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)
                else:
                    tmp2_installments_data_df = tmp1_installments_data_df[(tmp1_installments_data_df[order_type[0]] == order_type[1]) == order_type[2]]
                    suffix = f'd{day}_{order_type[0].split("_")[0]}{int(order_type[2])}'
                    self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)

            for time_type in time_type_list:
                tmp2_installments_data_df = tmp1_installments_data_df[(tmp1_installments_data_df['create_time_hour'] >= time_type[0]) & (tmp1_installments_data_df['create_time_hour'] <= time_type[1])]
                suffix = f'd{day}_time{time_type[0]}-{time_type[1]}'
                self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)

            for day_type in day_type_list:
                tmp2_installments_data_df = tmp1_installments_data_df[tmp1_installments_data_df[day_type[0]] == day_type[1]]
                suffix = f'd{day}_{day_type[0].split("_")[1]}{day_type[1]}'
                self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)

        app_order_id_sort = self.installments_data_df.sort_values('create_time', ascending=False)['app_order_id'].drop_duplicates()
        for cnt in cnt_list:
            tmp1_installments_data_df=self.installments_data_df[self.installments_data_df['app_order_id'].isin(app_order_id_sort.iloc[:cnt])]
            for order_type in order_type_list:
                if order_type == 'all':
                    tmp2_installments_data_df = tmp1_installments_data_df
                    suffix = f'cnt{cnt}_all'
                    self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)
                else:
                    tmp2_installments_data_df = tmp1_installments_data_df[(tmp1_installments_data_df[order_type[0]] == order_type[1]) == order_type[2]]
                    suffix = f'cnt{cnt}_{order_type[0].split("_")[0]}{int(order_type[2])}'
                    self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)

            for time_type in time_type_list:
                tmp2_installments_data_df = tmp1_installments_data_df[(tmp1_installments_data_df['create_time_hour'] >= time_type[0]) & (tmp1_installments_data_df['create_time_hour'] <= time_type[1])]
                suffix = f'cnt{cnt}_time{time_type[0]}-{time_type[1]}'
                self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)

            for day_type in day_type_list:
                tmp2_installments_data_df = tmp1_installments_data_df[tmp1_installments_data_df[day_type[0]] == day_type[1]]
                suffix = f'cnt{cnt}_{day_type[0].split("_")[1]}{day_type[1]}'
                self.__gen_onloan_feature(tmp2_installments_data_df,suffix,feature_dict)

        self.__gen_onloan_today_feature(self.contract_data_df,feature_dict)
        self.__gen_onloan_samegroup_feature(self.order_data_df, feature_dict)
        self.__gen_onloan_unexpired_feature(self.installments_data_df,feature_dict)

        for key, value in feature_dict.items():
            feature_dict[key] = float(value) if not np.isnan(value) else -999.0

        return feature_dict

    def __gen_onloan_feature(self, installments_data_df,suffix,feature_dict):
        # 在贷订单
        onloan_order_data_df = installments_data_df[installments_data_df['status'] == 1]
        onloan_order_num = onloan_order_data_df['app_order_id'].nunique()
        onloan_channel_num = onloan_order_data_df['acq_channel'].nunique()
        onloan_amount_sum = onloan_order_data_df['amount'].sum()
        onloan_amount_max = onloan_order_data_df['amount'].max()
        onloan_amount_min = onloan_order_data_df['amount'].min()
        onloan_amount_mean = onloan_order_data_df['amount'].mean()
        onloan_amount_std = onloan_order_data_df['amount'].std()
        # 在贷未到期订单
        onloan_unexpired_order_data_df = installments_data_df[(installments_data_df['repayment_date'] > self.order_apply_date) & (installments_data_df['status'] == 1)]
        onloan_unexpired_order_num = onloan_unexpired_order_data_df['app_order_id'].nunique()
        onloan_unexpired_channel_num = onloan_unexpired_order_data_df['acq_channel'].nunique()
        onloan_unexpired_amount_sum = onloan_unexpired_order_data_df['amount'].sum()
        onloan_unexpired_amount_max = onloan_unexpired_order_data_df['amount'].max()
        onloan_unexpired_amount_min = onloan_unexpired_order_data_df['amount'].min()
        onloan_unexpired_amount_mean = onloan_unexpired_order_data_df['amount'].mean()
        onloan_unexpired_amount_std = onloan_unexpired_order_data_df['amount'].std()

        # 在贷逾期订单
        onloan_ovd_order_data_df = installments_data_df[(installments_data_df['status'] == 1) & (installments_data_df['overdue_days'] > 0)]
        onloan_ovd_order_num = onloan_ovd_order_data_df['app_order_id'].nunique()
        onloan_ovd_channel_num = onloan_ovd_order_data_df['acq_channel'].nunique()
        onloan_ovd_amount_sum = onloan_ovd_order_data_df['amount'].sum()
        onloan_ovd_ovddays_sum = onloan_ovd_order_data_df['overdue_days'].sum()
        onloan_ovd_ovddays_max = onloan_ovd_order_data_df['overdue_days'].max()
        onloan_ovd_ovddays_min = onloan_ovd_order_data_df['overdue_days'].min()
        onloan_ovd_ovddays_mean = onloan_ovd_order_data_df['overdue_days'].mean()
        onloan_ovd_ovddays_std = onloan_ovd_order_data_df['overdue_days'].std()

        feature_dict[f'onloan_order_num_{suffix}'] = onloan_order_num
        feature_dict[f'onloan_channel_num_{suffix}'] = onloan_channel_num
        feature_dict[f'onloan_amount_sum_{suffix}'] = onloan_amount_sum
        feature_dict[f'onloan_amount_max_{suffix}'] = onloan_amount_max
        feature_dict[f'onloan_amount_min_{suffix}'] = onloan_amount_min
        feature_dict[f'onloan_amount_mean_{suffix}'] = onloan_amount_mean
        feature_dict[f'onloan_amount_std_{suffix}'] = onloan_amount_std
        feature_dict[f'onloan_unexpired_order_num_{suffix}'] = onloan_unexpired_order_num
        feature_dict[f'onloan_unexpired_channel_num_{suffix}'] = onloan_unexpired_channel_num
        feature_dict[f'onloan_unexpired_amount_sum_{suffix}'] = onloan_unexpired_amount_sum
        feature_dict[f'onloan_unexpired_amount_max_{suffix}'] = onloan_unexpired_amount_max
        feature_dict[f'onloan_unexpired_amount_min_{suffix}'] = onloan_unexpired_amount_min
        feature_dict[f'onloan_unexpired_amount_mean_{suffix}'] = onloan_unexpired_amount_mean
        feature_dict[f'onloan_unexpired_amount_std_{suffix}'] = onloan_unexpired_amount_std
        feature_dict[f'onloan_ovd_order_num_{suffix}'] = onloan_ovd_order_num
        feature_dict[f'onloan_ovd_channel_num_{suffix}'] = onloan_ovd_channel_num
        feature_dict[f'onloan_ovd_amount_sum_{suffix}'] = onloan_ovd_amount_sum
        feature_dict[f'onloan_ovd_ovddays_sum_{suffix}'] = onloan_ovd_ovddays_sum
        feature_dict[f'onloan_ovd_ovddays_max_{suffix}'] = onloan_ovd_ovddays_max
        feature_dict[f'onloan_ovd_ovddays_min_{suffix}'] = onloan_ovd_ovddays_min
        feature_dict[f'onloan_ovd_ovddays_mean_{suffix}'] = onloan_ovd_ovddays_mean
        feature_dict[f'onloan_ovd_ovddays_std_{suffix}'] = onloan_ovd_ovddays_std

    def __gen_onloan_today_feature(self, contract_data_df,feature_dict):
        # 当天相关
        # 当天放款订单
        onloan_today_loan_data_df = contract_data_df[contract_data_df['activation_date'] == self.order_apply_date]
        # 当天还款订单
        onloan_today_repay_data_df = contract_data_df[contract_data_df['settlement_date'] == self.order_apply_date]

        onloan_today_loan_order_num = onloan_today_loan_data_df['app_order_id'].nunique()
        onloan_today_loan_product_num = onloan_today_loan_data_df['product_code'].nunique()
        onloan_today_loan_amount_sum = onloan_today_loan_data_df['loan_amount'].sum()
        onloan_today_loan_channel_num = onloan_today_loan_data_df['acq_channel'].nunique()
        onloan_today_repay_order_num = onloan_today_repay_data_df['app_order_id'].nunique()
        onloan_today_repay_product_num = onloan_today_repay_data_df['product_code'].nunique()
        onloan_today_repay_amount_sum = onloan_today_repay_data_df['total_repaid_amount'].sum()
        onloan_today_repay_channel_num = onloan_today_repay_data_df['acq_channel'].nunique()

        # 放款和还款在同一天的一笔订单
        onloan_sameday_sameorder_loan_repay_data_df = contract_data_df[contract_data_df['activation_date'] == contract_data_df['settlement_date']]

        onloan_sameday_sameorder_order_num = onloan_sameday_sameorder_loan_repay_data_df['app_order_id'].nunique()
        onloan_sameday_sameorder_channel_num = onloan_sameday_sameorder_loan_repay_data_df['acq_channel'].nunique()
        onloan_sameday_sameorder_product_num = onloan_sameday_sameorder_loan_repay_data_df['product_code'].nunique()
        onloan_sameday_sameorder_order_active_settle_time_diff_mean = (pd.to_datetime(contract_data_df['settlement_time'],format="%Y-%m-%d %H:%M:%S")-pd.to_datetime(contract_data_df['activation_time'],format="%Y-%m-%d %H:%M:%S")).dt.total_seconds().mean()
        # 还款订单｜结清订单
        repay_data_df = contract_data_df[contract_data_df['status'] == 6]
        repay_data_df = repay_data_df[['app_order_id', 'settlement_date', 'settlement_time', 'loan_amount', 'product_code', 'acq_channel']]
        # 放款订单
        loan_data_df = contract_data_df[contract_data_df['status'].isin([4, 6])]
        loan_data_df = loan_data_df[['app_order_id', 'activation_date', 'activation_time', 'loan_amount']]
        # 放款和还款在同一天的不同订单
        onloan_sameday_difforder_loan_repay_data_df = pd.merge(repay_data_df, loan_data_df, left_on=['settlement_date'], right_on=['activation_date'])
        onloan_sameday_difforder_loan_repay_data_df = onloan_sameday_difforder_loan_repay_data_df[onloan_sameday_difforder_loan_repay_data_df['settlement_time'] < onloan_sameday_difforder_loan_repay_data_df['activation_time']]
        onloan_sameday_difforder_loan_repay_data_df['loan_amount_diff'] = onloan_sameday_difforder_loan_repay_data_df['loan_amount_y'] - onloan_sameday_difforder_loan_repay_data_df['loan_amount_x']
        onloan_sameday_difforder_loan_repay_data_df['loan_amount_ratio'] = onloan_sameday_difforder_loan_repay_data_df.apply(lambda x: divide(x['loan_amount_y'], x['loan_amount_x']), axis=1)
        onloan_sameday_difforder_loan_amount_diff_max = onloan_sameday_difforder_loan_repay_data_df['loan_amount_diff'].max()
        onloan_sameday_difforder_loan_amount_diff_min = onloan_sameday_difforder_loan_repay_data_df['loan_amount_diff'].min()
        onloan_sameday_difforder_loan_amount_diff_mean = onloan_sameday_difforder_loan_repay_data_df['loan_amount_diff'].mean()
        onloan_sameday_difforder_loan_amount_diff_std = onloan_sameday_difforder_loan_repay_data_df['loan_amount_diff'].std()
        onloan_sameday_difforder_loan_amount_ratio_max = onloan_sameday_difforder_loan_repay_data_df['loan_amount_ratio'].max()
        onloan_sameday_difforder_loan_amount_ratio_min = onloan_sameday_difforder_loan_repay_data_df['loan_amount_ratio'].min()
        onloan_sameday_difforder_loan_amount_ratio_mean = onloan_sameday_difforder_loan_repay_data_df['loan_amount_ratio'].mean()
        onloan_sameday_difforder_loan_amount_ratio_std = onloan_sameday_difforder_loan_repay_data_df['loan_amount_ratio'].std()

        feature_dict['onloan_today_loan_order_num'] = onloan_today_loan_order_num
        feature_dict['onloan_today_loan_product_num'] = onloan_today_loan_product_num
        feature_dict['onloan_today_loan_amount_sum'] = onloan_today_loan_amount_sum
        feature_dict['onloan_today_loan_channel_num'] = onloan_today_loan_channel_num
        feature_dict['onloan_today_repay_order_num'] = onloan_today_repay_order_num
        feature_dict['onloan_today_repay_product_num'] = onloan_today_repay_product_num
        feature_dict['onloan_today_repay_amount_sum'] = onloan_today_repay_amount_sum
        feature_dict['onloan_today_repay_channel_num'] = onloan_today_repay_channel_num
        feature_dict['onloan_sameday_sameorder_order_num'] = onloan_sameday_sameorder_order_num
        feature_dict['onloan_sameday_sameorder_channel_num'] = onloan_sameday_sameorder_channel_num
        feature_dict['onloan_sameday_sameorder_product_num'] = onloan_sameday_sameorder_product_num
        feature_dict['onloan_sameday_sameorder_order_active_settle_time_diff_mean'] = onloan_sameday_sameorder_order_active_settle_time_diff_mean
        feature_dict['onloan_sameday_difforder_loan_amount_diff_max'] = onloan_sameday_difforder_loan_amount_diff_max
        feature_dict['onloan_sameday_difforder_loan_amount_diff_min'] = onloan_sameday_difforder_loan_amount_diff_min
        feature_dict['onloan_sameday_difforder_loan_amount_diff_mean'] = onloan_sameday_difforder_loan_amount_diff_mean
        feature_dict['onloan_sameday_difforder_loan_amount_diff_std'] = onloan_sameday_difforder_loan_amount_diff_std
        feature_dict['onloan_sameday_difforder_loan_amount_ratio_max'] = onloan_sameday_difforder_loan_amount_ratio_max
        feature_dict['onloan_sameday_difforder_loan_amount_ratio_min'] = onloan_sameday_difforder_loan_amount_ratio_min
        feature_dict['onloan_sameday_difforder_loan_amount_ratio_mean'] = onloan_sameday_difforder_loan_amount_ratio_mean
        feature_dict['onloan_sameday_difforder_loan_amount_ratio_std'] = onloan_sameday_difforder_loan_amount_ratio_std

    def __gen_onloan_samegroup_feature(self, order_data_df,feature_dict):
        # 审核订单
        if self.app_order_id!='null':
            onloan_samegroup_order_data_df = order_data_df[(order_data_df['apply_time'] == self.order_apply_time)&(order_data_df['app_order_id'] < int(self.app_order_id))]
            onloan_samegroup_pass_order_num = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'app_order_id'].nunique()
            onloan_samegroup_pass_product_num = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'product_code'].nunique()
            onloan_samegroup_pass_channel_num = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'acq_channel'].nunique()
            onloan_samegroup_pass_amount_sum = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'loan_amount'].sum()
            onloan_samegroup_pass_amount_max = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'loan_amount'].max()
            onloan_samegroup_pass_amount_min = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'loan_amount'].min()
            onloan_samegroup_pass_amount_mean = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'loan_amount'].mean()
            onloan_samegroup_pass_amount_std = onloan_samegroup_order_data_df.loc[onloan_samegroup_order_data_df['status'] == 21, 'loan_amount'].std()
        else:
            onloan_samegroup_pass_order_num = -999
            onloan_samegroup_pass_product_num = -999
            onloan_samegroup_pass_channel_num = -999
            onloan_samegroup_pass_amount_sum = -999
            onloan_samegroup_pass_amount_max = -999
            onloan_samegroup_pass_amount_min = -999
            onloan_samegroup_pass_amount_mean = -999
            onloan_samegroup_pass_amount_std = -999

        feature_dict['onloan_samegroup_pass_order_num'] = onloan_samegroup_pass_order_num
        feature_dict['onloan_samegroup_pass_product_num'] = onloan_samegroup_pass_product_num
        feature_dict['onloan_samegroup_pass_channel_num'] = onloan_samegroup_pass_channel_num
        feature_dict['onloan_samegroup_pass_amount_sum'] = onloan_samegroup_pass_amount_sum
        feature_dict['onloan_samegroup_pass_amount_max'] = onloan_samegroup_pass_amount_max
        feature_dict['onloan_samegroup_pass_amount_min'] = onloan_samegroup_pass_amount_min
        feature_dict['onloan_samegroup_pass_amount_mean'] = onloan_samegroup_pass_amount_mean
        feature_dict['onloan_samegroup_pass_amount_std'] = onloan_samegroup_pass_amount_std

    def __gen_onloan_unexpired_feature(self, installments_data_df,feature_dict):

        def cum_repaydate_pressure(x):
            repaydate_order_num = x['app_order_id'].nunique()
            repaydate_amount_sum = x['amount'].sum()
            repayment_date = x['repayment_date'].max()
            repaydate_hour_diff = (datetime.strptime(repayment_date, "%Y-%m-%d") - self.order_apply_time_datetime).total_seconds() / 3600
            repaydate_order_pressure = divide(repaydate_order_num, repaydate_hour_diff)
            repaydate_amount_pressure = divide(repaydate_amount_sum, repaydate_hour_diff)
            return pd.Series(
                {'repaydate_order_num': repaydate_order_num, 'repaydate_amount_sum': repaydate_amount_sum,
                 'repaydate_hour_diff': repaydate_hour_diff, 'repaydate_order_pressure': repaydate_order_pressure,
                 'repaydate_amount_pressure': repaydate_amount_pressure})

        onloan_unexpired_order_data_df = installments_data_df[(installments_data_df['repayment_date'] > self.order_apply_date) & (installments_data_df['status'] == 1)]
        if not onloan_unexpired_order_data_df.empty:
            onloan_repaydate_pressure_df = onloan_unexpired_order_data_df.groupby('repayment_date').apply(cum_repaydate_pressure).reset_index()
            onloan_repaydate_pressure_df_sort = onloan_repaydate_pressure_df.sort_values('repayment_date')
            # 最近还款点(在贷未到期订单)
            onloan_min_repaydate_order = onloan_repaydate_pressure_df_sort.iloc[0]

            onloan_min_repaydate_order_num = onloan_min_repaydate_order['repaydate_order_num']
            onloan_min_repaydate_amount_sum = onloan_min_repaydate_order['repaydate_amount_sum']
            onloan_min_repaydate_hour_diff = onloan_min_repaydate_order['repaydate_hour_diff']
            onloan_min_repaydate_order_pressure = onloan_min_repaydate_order['repaydate_order_pressure']
            onloan_min_repaydate_amount_pressure = onloan_min_repaydate_order['repaydate_amount_pressure']

            # 最远还款点(在贷未到期订单)
            onloan_max_repaydate_order = onloan_repaydate_pressure_df_sort.iloc[-1]

            onloan_max_repaydate_order_num = onloan_max_repaydate_order['repaydate_order_num']
            onloan_max_repaydate_amount_sum = onloan_max_repaydate_order['repaydate_amount_sum']
            onloan_max_repaydate_hour_diff = onloan_max_repaydate_order['repaydate_hour_diff']
            onloan_max_repaydate_order_pressure = onloan_max_repaydate_order['repaydate_order_pressure']
            onloan_max_repaydate_amount_pressure = onloan_max_repaydate_order['repaydate_amount_pressure']

            max_repaydate = onloan_max_repaydate_order['repayment_date']
            onloan_in_max_repaydate_num = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_repaydate, 'repayment_date'].nunique()
            onloan_in_max_repaydate_order_num = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_repaydate, 'repaydate_order_num'].sum()
            onloan_in_max_repaydate_amount_sum = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_repaydate, 'repaydate_amount_sum'].sum()

            # 订单压力最大还款点(在贷未到期订单)
            onloan_max_cnt_repaydate_order = onloan_repaydate_pressure_df.sort_values('repaydate_order_num', ascending=False).iloc[0]

            onloan_max_cnt_repaydate_order_num = onloan_max_cnt_repaydate_order['repaydate_order_num']
            onloan_max_cnt_repaydate_amount_sum = onloan_max_cnt_repaydate_order['repaydate_amount_sum']
            onloan_max_cnt_repaydate_hour_diff = onloan_max_cnt_repaydate_order['repaydate_hour_diff']
            onloan_max_cnt_repaydate_order_pressure = onloan_max_cnt_repaydate_order['repaydate_order_pressure']
            onloan_max_cnt_repaydate_amount_pressure = onloan_max_cnt_repaydate_order['repaydate_amount_pressure']

            max_cnt_repaydate = onloan_max_cnt_repaydate_order['repayment_date']
            onloan_in_max_cnt_repaydate_num = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_cnt_repaydate, 'repayment_date'].nunique()
            onloan_in_max_cnt_repaydate_order_num = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_cnt_repaydate, 'repaydate_order_num'].sum()
            onloan_in_max_cnt_repaydate_amount_sum = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_cnt_repaydate, 'repaydate_amount_sum'].sum()

            # 金额压力最大还款点(在贷未到期订单)
            onloan_max_sum_repaydate_order = onloan_repaydate_pressure_df.sort_values('repaydate_amount_sum', ascending=False).iloc[0]

            onloan_max_sum_repaydate_order_num = onloan_max_sum_repaydate_order['repaydate_order_num']
            onloan_max_sum_repaydate_amount_sum = onloan_max_sum_repaydate_order['repaydate_amount_sum']
            onloan_max_sum_repaydate_hour_diff = onloan_max_sum_repaydate_order['repaydate_hour_diff']
            onloan_max_sum_repaydate_order_pressure = onloan_max_sum_repaydate_order['repaydate_order_pressure']
            onloan_max_sum_repaydate_amount_pressure = onloan_max_sum_repaydate_order['repaydate_amount_pressure']

            max_sum_repaydate = onloan_max_sum_repaydate_order['repayment_date']
            onloan_in_max_sum_repaydate_num = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_sum_repaydate, 'repayment_date'].nunique()
            onloan_in_max_sum_repaydate_order_num = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_sum_repaydate, 'repaydate_order_num'].sum()
            onloan_in_max_sum_repaydate_amount_sum = onloan_repaydate_pressure_df.loc[onloan_repaydate_pressure_df['repayment_date'] < max_sum_repaydate, 'repaydate_amount_sum'].sum()

            # 累计还款点
            onloan_repaydate_order_pressure_sum = onloan_repaydate_pressure_df['repaydate_order_pressure'].sum()
            onloan_repaydate_order_pressure_max = onloan_repaydate_pressure_df['repaydate_order_pressure'].max()
            onloan_repaydate_order_pressure_min = onloan_repaydate_pressure_df['repaydate_order_pressure'].min()
            onloan_repaydate_order_pressure_mean = onloan_repaydate_pressure_df['repaydate_order_pressure'].mean()
            onloan_repaydate_order_pressure_std = onloan_repaydate_pressure_df['repaydate_order_pressure'].std()
            onloan_repaydate_amount_pressure_sum = onloan_repaydate_pressure_df['repaydate_amount_pressure'].sum()
            onloan_repaydate_amount_pressure_max = onloan_repaydate_pressure_df['repaydate_amount_pressure'].max()
            onloan_repaydate_amount_pressure_min = onloan_repaydate_pressure_df['repaydate_amount_pressure'].min()
            onloan_repaydate_amount_pressure_mean = onloan_repaydate_pressure_df['repaydate_amount_pressure'].mean()
            onloan_repaydate_amount_pressure_std = onloan_repaydate_pressure_df['repaydate_amount_pressure'].std()
        else:
            onloan_repaydate_pressure_df = pd.DataFrame(columns=['repayment_date', 'repaydate_order_num', 'repaydate_amount_sum', 'repaydate_hour_diff', 'repaydate_order_pressure', 'repaydate_amount_pressure'])
            onloan_min_repaydate_order_num = float('nan')
            onloan_min_repaydate_amount_sum = float('nan')
            onloan_min_repaydate_hour_diff = float('nan')
            onloan_min_repaydate_order_pressure = float('nan')
            onloan_min_repaydate_amount_pressure = float('nan')
            onloan_max_repaydate_order_num = float('nan')
            onloan_max_repaydate_amount_sum = float('nan')
            onloan_max_repaydate_hour_diff = float('nan')
            onloan_max_repaydate_order_pressure = float('nan')
            onloan_max_repaydate_amount_pressure = float('nan')
            onloan_in_max_repaydate_num = float('nan')
            onloan_in_max_repaydate_order_num = float('nan')
            onloan_in_max_repaydate_amount_sum = float('nan')
            onloan_max_cnt_repaydate_order_num = float('nan')
            onloan_max_cnt_repaydate_amount_sum = float('nan')
            onloan_max_cnt_repaydate_hour_diff = float('nan')
            onloan_max_cnt_repaydate_order_pressure = float('nan')
            onloan_max_cnt_repaydate_amount_pressure = float('nan')
            onloan_in_max_cnt_repaydate_num = float('nan')
            onloan_in_max_cnt_repaydate_order_num = float('nan')
            onloan_in_max_cnt_repaydate_amount_sum = float('nan')
            onloan_max_sum_repaydate_order_num = float('nan')
            onloan_max_sum_repaydate_amount_sum = float('nan')
            onloan_max_sum_repaydate_hour_diff = float('nan')
            onloan_max_sum_repaydate_order_pressure = float('nan')
            onloan_max_sum_repaydate_amount_pressure = float('nan')
            onloan_in_max_sum_repaydate_num = float('nan')
            onloan_in_max_sum_repaydate_order_num = float('nan')
            onloan_in_max_sum_repaydate_amount_sum = float('nan')
            onloan_repaydate_order_pressure_sum = float('nan')
            onloan_repaydate_order_pressure_max = float('nan')
            onloan_repaydate_order_pressure_min = float('nan')
            onloan_repaydate_order_pressure_mean = float('nan')
            onloan_repaydate_order_pressure_std = float('nan')
            onloan_repaydate_amount_pressure_sum = float('nan')
            onloan_repaydate_amount_pressure_max = float('nan')
            onloan_repaydate_amount_pressure_min = float('nan')
            onloan_repaydate_amount_pressure_mean = float('nan')
            onloan_repaydate_amount_pressure_std = float('nan')

        # 审核过后的新增压力
        current_repaydate = str(self.order_apply_time_datetime + timedelta(days=self.current_days_per_term * self.current_term - 1))[:10]
        if current_repaydate in onloan_repaydate_pressure_df['repayment_date'].tolist():
            onloan_current_repaydate_order = onloan_repaydate_pressure_df[onloan_repaydate_pressure_df['repayment_date'] == current_repaydate].iloc[0]
            onloan_current_repaydate_order_num = onloan_current_repaydate_order['repaydate_order_num'] + 1
            onloan_current_repaydate_amount_sum = onloan_current_repaydate_order['repaydate_amount_sum'] + self.current_amount
        else:
            onloan_current_repaydate_order_num = 1
            onloan_current_repaydate_amount_sum = self.current_amount

        onloan_current_repaydate_hour_diff = (datetime.strptime(current_repaydate, "%Y-%m-%d") - self.order_apply_time_datetime).total_seconds() / 3600
        onloan_current_repaydate_order_pressure = divide(onloan_current_repaydate_order_num, onloan_current_repaydate_hour_diff)
        onloan_current_repaydate_amount_pressure = divide(onloan_current_repaydate_amount_sum, onloan_current_repaydate_hour_diff)

        onloan_order_num = feature_dict['onloan_order_num_d360_all']
        onloan_samegroup_pass_order_num = feature_dict['onloan_samegroup_pass_order_num']
        onloan_samegroup_pass_amount_sum = feature_dict['onloan_samegroup_pass_amount_sum']

        onloan_samegroup_pass_onloan_order_num_ratio = divide(onloan_samegroup_pass_order_num, onloan_order_num)
        onloan_samegroup_pass_order_num_ratio = divide(onloan_samegroup_pass_order_num + 1, onloan_repaydate_pressure_df['repaydate_order_num'].sum())
        onloan_samegroup_pass_amount_sum_ratio = divide(onloan_samegroup_pass_amount_sum + self.current_amount, onloan_repaydate_pressure_df['repaydate_amount_sum'].sum())

        feature_dict['onloan_min_repaydate_order_num'] = onloan_min_repaydate_order_num
        feature_dict['onloan_min_repaydate_amount_sum'] = onloan_min_repaydate_amount_sum
        feature_dict['onloan_min_repaydate_hour_diff'] = onloan_min_repaydate_hour_diff
        feature_dict['onloan_min_repaydate_order_pressure'] = onloan_min_repaydate_order_pressure
        feature_dict['onloan_min_repaydate_amount_pressure'] = onloan_min_repaydate_amount_pressure
        feature_dict['onloan_max_repaydate_order_num'] = onloan_max_repaydate_order_num
        feature_dict['onloan_max_repaydate_amount_sum'] = onloan_max_repaydate_amount_sum
        feature_dict['onloan_max_repaydate_hour_diff'] = onloan_max_repaydate_hour_diff
        feature_dict['onloan_max_repaydate_order_pressure'] = onloan_max_repaydate_order_pressure
        feature_dict['onloan_max_repaydate_amount_pressure'] = onloan_max_repaydate_amount_pressure
        feature_dict['onloan_in_max_repaydate_num'] = onloan_in_max_repaydate_num
        feature_dict['onloan_in_max_repaydate_order_num'] = onloan_in_max_repaydate_order_num
        feature_dict['onloan_in_max_repaydate_amount_sum'] = onloan_in_max_repaydate_amount_sum
        feature_dict['onloan_max_cnt_repaydate_order_num'] = onloan_max_cnt_repaydate_order_num
        feature_dict['onloan_max_cnt_repaydate_amount_sum'] = onloan_max_cnt_repaydate_amount_sum
        feature_dict['onloan_max_cnt_repaydate_hour_diff'] = onloan_max_cnt_repaydate_hour_diff
        feature_dict['onloan_max_cnt_repaydate_order_pressure'] = onloan_max_cnt_repaydate_order_pressure
        feature_dict['onloan_max_cnt_repaydate_amount_pressure'] = onloan_max_cnt_repaydate_amount_pressure
        feature_dict['onloan_in_max_cnt_repaydate_num'] = onloan_in_max_cnt_repaydate_num
        feature_dict['onloan_in_max_cnt_repaydate_order_num'] = onloan_in_max_cnt_repaydate_order_num
        feature_dict['onloan_in_max_cnt_repaydate_amount_sum'] = onloan_in_max_cnt_repaydate_amount_sum
        feature_dict['onloan_max_sum_repaydate_order_num'] = onloan_max_sum_repaydate_order_num
        feature_dict['onloan_max_sum_repaydate_amount_sum'] = onloan_max_sum_repaydate_amount_sum
        feature_dict['onloan_max_sum_repaydate_hour_diff'] = onloan_max_sum_repaydate_hour_diff
        feature_dict['onloan_max_sum_repaydate_order_pressure'] = onloan_max_sum_repaydate_order_pressure
        feature_dict['onloan_max_sum_repaydate_amount_pressure'] = onloan_max_sum_repaydate_amount_pressure
        feature_dict['onloan_in_max_sum_repaydate_num'] = onloan_in_max_sum_repaydate_num
        feature_dict['onloan_in_max_sum_repaydate_order_num'] = onloan_in_max_sum_repaydate_order_num
        feature_dict['onloan_in_max_sum_repaydate_amount_sum'] = onloan_in_max_sum_repaydate_amount_sum
        feature_dict['onloan_repaydate_order_pressure_sum'] = onloan_repaydate_order_pressure_sum
        feature_dict['onloan_repaydate_order_pressure_max'] = onloan_repaydate_order_pressure_max
        feature_dict['onloan_repaydate_order_pressure_min'] = onloan_repaydate_order_pressure_min
        feature_dict['onloan_repaydate_order_pressure_mean'] = onloan_repaydate_order_pressure_mean
        feature_dict['onloan_repaydate_order_pressure_std'] = onloan_repaydate_order_pressure_std
        feature_dict['onloan_repaydate_amount_pressure_sum'] = onloan_repaydate_amount_pressure_sum
        feature_dict['onloan_repaydate_amount_pressure_max'] = onloan_repaydate_amount_pressure_max
        feature_dict['onloan_repaydate_amount_pressure_min'] = onloan_repaydate_amount_pressure_min
        feature_dict['onloan_repaydate_amount_pressure_mean'] = onloan_repaydate_amount_pressure_mean
        feature_dict['onloan_repaydate_amount_pressure_std'] = onloan_repaydate_amount_pressure_std
        feature_dict['onloan_samegroup_pass_onloan_order_num_ratio'] = onloan_samegroup_pass_onloan_order_num_ratio
        feature_dict['onloan_current_repaydate_order_pressure'] = onloan_current_repaydate_order_pressure
        feature_dict['onloan_current_repaydate_amount_pressure'] = onloan_current_repaydate_amount_pressure
        feature_dict['onloan_samegroup_pass_order_num_ratio'] = onloan_samegroup_pass_order_num_ratio
        feature_dict['onloan_samegroup_pass_amount_sum_ratio'] = onloan_samegroup_pass_amount_sum_ratio
